package leecode

func searchMatrix(matrix [][]int, target int) bool {
	var row int
	//确定target所在的行
	for i := 0; i < len(matrix); i++ {
		if matrix[i][0] <= target && matrix[i][len(matrix[0])-1] >= target {
			row = i
			break
		}
	}
	left := 0
	right := len(matrix[0]) - 1
	for left <= right {
		mid := (left + right) >> 1
		if target > matrix[row][mid] {
			left = mid + 1
		} else if matrix[row][mid] > target {
			right = mid - 1
		} else {
			return true
		}
	}
	return false
}
